import {getValByPath} from '../../utils/tools'

type IState = {
  [k:string]:any
}

const state=() => ({ 
  token:'',
  roles:[],
  user:null,
  address:null
})
const mutations={ 
  setRoles(state:IState,data:string[]){
    state.roles=data
  },
  addRole(state:IState,data:string){
    state.roles.push(data)
  },
  setToken(state:IState,data:string){
    state.token=data
  },
  setUser(state:IState,data:string){
    state.user=data
  },
  setAddress(state:IState,data:string){
    state.address=data
  },
  clear(state:IState){
    state.token=''
    state.user=null
    state.roles=[]
    state.address=null
  }
}
const actions={ 
  
}
const getters={ 
  getRoles(state:IState){
    return state.roles
  },
  hasRole(state:IState){
    return (role:string) =>{
      return state.roles.includes(role)
    }
  },
  getToken(state:IState){
    return state.token
  },
  getUser(state:IState){
    return (path,defaultVal='') => getValByPath(state.user,path,defaultVal)
  },
  hasLogin(state:IState){
    return state.token?true:false
  },
  getAddress(state:IState){
    return state.address
  }
}
const namespaced=true
export {
  state,mutations,actions,getters,namespaced
}